import json
import os

# 设置 JSON 文件目录
json_dir = r'F:\Frames\tissue_bag\tissue_bag_3_hyr\tissue_bag2\label'  # 替换为你的 JSON 文件目录
image_dir = r'F:\Frames\tissue_bag\images\train'  # 替换为你的图片文件目录

# 获取所有 JSON 文件并按数字顺序排序
json_files = sorted([f for f in os.listdir(json_dir) if f.endswith('.json')], key=lambda x: int(x.split('.')[0]))

# 遍历每个 JSON 文件并修改其中的 imagePath
for index, json_file in enumerate(json_files, start=191):
    # 读取 JSON 文件
    json_path = os.path.join(json_dir, json_file)
    with open(json_path, 'r', encoding='utf-8') as f:
        data = json.load(f)

    # 修改 imagePath
    image_filename = f"{index}.jpg"
    new_image_path = os.path.join(image_dir, image_filename)

    if 'imagePath' in data:
        data['imagePath'] = new_image_path  # 更新图片路径

    # 保存修改后的 JSON 文件
    with open(json_path, 'w', encoding='utf-8') as f:
        json.dump(data, f, indent=4, ensure_ascii=False)

    print(f"已更新文件: {json_file} 到 {new_image_path}")
